package com.open.party.cloud.view.examine.examineForGanbu.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import cn.android.x.model.data.ResultInfo
import cn.android.x.parent.AppRecycleViewBaseFragment
import com.open.party.cloud.R
import com.open.party.cloud.model.ExamineBean
import com.open.party.cloud.view.examine.ExamineDoingActivity
import com.open.party.cloud.view.examine.examineForGanbu.adpater.ExamineForMoNiSjListAdapter
import com.open.party.cloud.viewModel.ExamineViewModel
import com.sinothk.android.utils.XUtils
import com.sinothk.android.utils.inters.OnSuperListener
import com.sinothk.rxretrofit.bean.PageVo
import kotlinx.android.synthetic.main.comm_fragment_rv_loading_list.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode


class ExamineForGanBuMoNiSjListFragment : AppRecycleViewBaseFragment<ExamineBean>(), OnSuperListener<ExamineBean> {

    private var viewModel: ExamineViewModel? = null
    private var adapter: ExamineForMoNiSjListAdapter? = null
    private var currView: View? = null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        if (currView == null) {
            currView = inflater.inflate(R.layout.comm_fragment_rv_loading_list, container, false)
        }
        return currView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        viewModel = ExamineViewModel()

        initRecycleLinearView(loadingRecyclerView)

        adapter = ExamineForMoNiSjListAdapter(activity)
        loadingRecyclerView.adapter = adapter
        adapter?.setOnSuperListener(this)

        loadingTipView.setRetryListener {
            loadingTipView.showLoading("努力加载中")
            refreshData()
        }

        loadingTipView.showLoading("努力加载中")

    }

    override fun onResume() {
        super.onResume()
        refreshData()
    }

    override fun loadData(pageVo: PageVo<ExamineBean>) {
        viewModel!!.getSimulationExamList()
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusCallback(result: ResultInfo<ArrayList<ExamineBean>>) {
        if ("getSimulationExamList" != result.eventType) {
            return
        }

        stopLoading(loadingRecyclerView, loadType)

        when (result.code) {
            ResultInfo.SUCCESS -> {
                if (result.data != null) {
                    if (result.data != null) {
                        if (result.data.size == 0) {
                            loadingTipView.showEmpty("暂无数据", R.drawable.empty_nodata)
                            loadingRecyclerView.setLoadingMoreEnabled(false)
                        } else {
                            adapter!!.setData(result.data!!)
                            loadingTipView.showContent()
                        }
                    } else {
                        loadingTipView.showError("数据异常")
                    }
                } else {
                    loadingTipView.showError("数据异常")
                }
            }
            else -> {
                loadingTipView.showError(result.msg)
            }
        }
    }


    override fun onClick(position: Int, itemData: ExamineBean, flag: String?) {
        if ("已结束" == itemData.examinationState) {
            XUtils.toast().show("考试已结束")
        } else {
            XUtils.intent().openActivity(activity, ExamineDoingActivity::class.java)
                .putStringExtra("examinationId", itemData.id)
                .start()
        }
    }
}